<template>
  <div 
    class="class-role"  v-if="switchOpen"            
    v-loading.fullscreen.lock="loading"
    element-loading-text="拼命加载中">
    <!--搜索框 -->
    <search></search>
    <!-- 角色列表 -->
    <component-table :tableData='tableData' :pData='data'></component-table>
    <!-- 分页 -->
    <common-pageination 
      :pageNo="pageNo"
      :total="total"
      :pageSize="pageSize">
    </common-pageination>
  </div>
</template>

<script>
  import Api from '@/api/Api'
  import axios from 'axios'
  import {formatDate} from '@/utils/Utils'
  import componentTable from './component/Table'
  import Bus from './component/Bus'
  import Search from './component/Search'
  import CommonPageination from './component/Pagination'
  export default {
    components: {
      componentTable,
      Search,
      CommonPageination
    },
    data() {
      return {
        //角色列表数据
        tableData: [],
        //角色授权数据
        data : [],
        //发送绑定的数据
        sendData: [],
        //当前页面
        pageNo: 1,
        //长度
        pageSize: 10,
        //总数
        total: 1,
        //授权角色id
        roleId: '',
        loading: false,
        switchOpen:false
      }
    },
    methods: {
      // 获取角色列表数据
      getRoleList(val,likeName) {
        let state = this.$store.state
        let params = {organizationId:state.user.organizationId,userId:state.user.userId,pageNo: val || 1,likeName: likeName}
        Api.role.find(params).then( res => {
          if(res.data.code == 0){
              let data = res.data.data.data
              this.pageNo = data.pageNum
              this.tableData = data.list
              this.total = data.total
              this.pageSize = data.pageSize
              this.switchOpen = true
          }else{
            return this.$message.error(res.data.msg)
          }
        })
      },
      // 获取角色授权数据
      getAuthorizationList() {
        let state = this.$store.state
        Api.role.findAuthorization({id:state.user.roleId}).then(res =>{
            if(res.data.code == 0){
                if(res.data.data.id == '' || res.data.data.id == null){
                    this.emptyText = '暂无内容'
                }else{
                    let resDate = res.data.data
                    this.data.push(resDate)
                }
            }else{
              return this.$message.error(res.data.msg)
          }
        })  
      }
    },
    mounted (){
      this.loading = true
      this.getRoleList()
      this.getAuthorizationList()
      this.$nextTick(() =>{
        setTimeout(() => {
          this.loading = false
        }, 500);
      })
      // 监听bus中loading状态
      Bus.$on('open-v-loading',(msg) =>{
          this.loading = true    
      })
      Bus.$on('close-v-loading',(msg,type) =>{
          let types = type == undefined ? 'success' : type
          this.getRoleList(this.pageNo)
          setTimeout(() => {
            this.loading = false
            this.$message({type: types,message: msg});
          }, 400);
      })
      Bus.$on('getRoleList',(val,likeName) =>{
        // 开启login
        this.loading = true
        this.getRoleList(val,likeName)
        setTimeout(() => {
            this.loading = false
        }, 400);
      })
    }
  }
</script>
